Skip to content

Feature: URL and document context attachments#105

Merged
slashdevcorpse merged 5 commits into
mainfrom
feature/url-document-context
May 31, 2026
Merged

Feature: URL and document context attachments#105
slashdevcorpse merged 5 commits into
mainfrom
feature/url-document-context

Conversation

@slashdevcorpse
Copy link
Copy Markdown
Owner

@slashdevcorpse slashdevcorpse commented May 31, 2026

Summary

  • add an explicit /api/context-preview endpoint for URL and local document context previews
  • support bounded previews for HTTP(S), markdown, text, JSON, and small text-based PDFs before send
  • wire reviewed context attachments into the composer, optimistic sends, pending sends, and /api/send prompt context

Files

  • apps/codex-claw/src/server/context-attachments.ts - bounded preview/extraction, send-time validation, and prompt assembly
  • apps/codex-claw/src/routes/api/context-preview.ts - preview API route
  • apps/codex-claw/src/routes/api/send.ts - includes reviewed context attachments in prompt context
  • apps/codex-claw/src/screens/chat/components/context-attachment-picker.tsx - URL/document preview and attach UI
  • apps/codex-claw/src/screens/chat/components/chat-composer.tsx - composer state and toolbar integration
  • apps/codex-claw/src/screens/chat/chat-screen.tsx and pending send files - payload carryover into sends
  • apps/codex-claw/src/server/context-attachments.test.ts - extraction, rejection, and prompt assembly coverage

Testing

  • pnpm -C apps/codex-claw lint
  • pnpm -C apps/codex-claw test
  • pnpm -C apps/codex-claw build

Risk

  • URL previews are explicit and bounded, but still perform server-side HTTP(S) fetches for the requested preview.
  • PDF extraction is intentionally limited to small text-based PDFs; scanned/compressed PDFs return an actionable error.

Closes #92


Summary by cubic

Adds URL and document context attachments with safe, bounded previews and sends them as prompt context so users can attach web pages or files to a chat. Includes a composer UI to preview and attach up to six items, plus server-side extraction, validation, and tests.

  • New Features

    • Added POST /api/context-preview to preview URL or document context.
    • Supports HTTP(S) pages, markdown, text, JSON, and small text-based PDFs; returns clear errors for unsupported/large content.
    • New composer controls to preview, add/remove, and summarize attachments; optimistic and pending sends carry contextAttachments.
    • /api/send parses reviewed attachments and builds a prompt block via server utilities, merged with repository context when present.
    • Server enforces size/token bounds, strips scripts/styles and normalizes HTML; extracts basic PDF text; full test coverage.
  • Bug Fixes

    • Hardened HTML filtering for URL previews to handle malformed tags and prevent content bleed.

Written for commit 8ac096f. Summary will update on new commits.

Review in cubic

Copilot AI review requested due to automatic review settings May 31, 2026 09:56
Comment thread apps/codex-claw/src/server/context-attachments.ts Fixed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Comment thread apps/codex-claw/src/server/context-attachments.ts Fixed
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 issues found across 13 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/codex-claw/src/server/context-attachments.ts">

<violation number="1" location="apps/codex-claw/src/server/context-attachments.ts:149">
P2: Guard decodeURIComponent here. Bad `%` encoding throws and breaks preview. Fallback to raw pathname when decode fails.</violation>

<violation number="2" location="apps/codex-claw/src/server/context-attachments.ts:339">
P1: Add host validation before fetch. Protocol-only check still allows SSRF to internal endpoints. Reject localhost/private network targets (and redirects to them) before downloading preview.</violation>
</file>

<file name="apps/codex-claw/src/screens/chat/components/context-attachment-picker.tsx">

<violation number="1" location="apps/codex-claw/src/screens/chat/components/context-attachment-picker.tsx:180">
P2: Check file size before base64 encode. Big file now gets fully read in browser first. Add early limit guard and fail fast.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

throw new Error('URL context must be a valid http or https URL.')
}

if (url.protocol !== 'http:' && url.protocol !== 'https:') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Add host validation before fetch. Protocol-only check still allows SSRF to internal endpoints. Reject localhost/private network targets (and redirects to them) before downloading preview.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/codex-claw/src/server/context-attachments.ts, line 339:

<comment>Add host validation before fetch. Protocol-only check still allows SSRF to internal endpoints. Reject localhost/private network targets (and redirects to them) before downloading preview.</comment>

<file context>
@@ -0,0 +1,587 @@
+    throw new Error('URL context must be a valid http or https URL.')
+  }
+
+  if (url.protocol !== 'http:' && url.protocol !== 'https:') {
+    throw new Error('URL context only supports http and https links.')
+  }
</file context>


function titleFromUrl(url: URL) {
const pathname = url.pathname.split('/').filter(Boolean).pop() ?? ''
const decoded = pathname ? decodeURIComponent(pathname) : ''
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Guard decodeURIComponent here. Bad % encoding throws and breaks preview. Fallback to raw pathname when decode fails.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/codex-claw/src/server/context-attachments.ts, line 149:

<comment>Guard decodeURIComponent here. Bad `%` encoding throws and breaks preview. Fallback to raw pathname when decode fails.</comment>

<file context>
@@ -0,0 +1,587 @@
+
+function titleFromUrl(url: URL) {
+  const pathname = url.pathname.split('/').filter(Boolean).pop() ?? ''
+  const decoded = pathname ? decodeURIComponent(pathname) : ''
+  return cleanTitle(decoded.replace(/[-_]+/g, ' ')) || url.hostname
+}
</file context>

setError(null)
setDraft(null)
try {
const content = await fileToBase64(file)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Check file size before base64 encode. Big file now gets fully read in browser first. Add early limit guard and fail fast.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/codex-claw/src/screens/chat/components/context-attachment-picker.tsx, line 180:

<comment>Check file size before base64 encode. Big file now gets fully read in browser first. Add early limit guard and fail fast.</comment>

<file context>
@@ -0,0 +1,354 @@
+    setError(null)
+    setDraft(null)
+    try {
+      const content = await fileToBase64(file)
+      setDraft(
+        await previewContextAttachment({
</file context>

Comment thread apps/codex-claw/src/server/context-attachments.ts Outdated
@slashdevcorpse slashdevcorpse merged commit b1dc91b into main May 31, 2026
5 checks passed
@slashdevcorpse slashdevcorpse deleted the feature/url-document-context branch May 31, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: URL and document context attachments

3 participants